package view;

import controller.MyController;
import lombok.SneakyThrows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;


public class UserLogin extends JFrame implements ActionListener {
    JButton btn1,btn2;     // btn1 注册，btn2 登录
    JPanel panel1,panel2,panel3;
    JLabel label1,label2;  // label1 用户名标签，label2 密码标签
    JTextField textField;  // 用户名输入框
    JPasswordField passwordField; // 密码输入框
    public UserLogin(){
        super("测试登录入口");
        ImageIcon icon = new ImageIcon("src\\main\\resources\\img\\icon6.png");
        this.setIconImage(icon.getImage());
        label1 = new JLabel("用户名");
        textField = new JTextField(20);
        panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel1.add(label1);
        panel1.add(textField);
        label2 = new JLabel("密   码");
        passwordField = new JPasswordField(20);
        panel2 = new JPanel();
        panel2.setLayout(new FlowLayout());
        panel2.add(label2);
        panel2.add(passwordField);
        btn1 = new JButton("登录");
        btn1.setBackground(Color.PINK);
        btn2 = new JButton("注册");
        btn2.setBackground(Color.lightGray);
        panel3 = new JPanel();
        panel3.setLayout(new FlowLayout());
        panel3.add(btn1);
        panel3.add(btn2);
        this.setLayout(new BorderLayout());
        this.add(panel1,BorderLayout.NORTH);
        this.add(panel2,BorderLayout.CENTER);
        this.add(panel3,BorderLayout.SOUTH);
        this.setBounds(500,250,300,140);
        this.setVisible(true);
        this.setResizable(false);       // 窗体大小不可改变
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
    }
    @SneakyThrows
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn1){
            if (MyController.UserLoginCheck(this,textField.getText(),String.valueOf(passwordField.getPassword())) == null){
                JOptionPane.showMessageDialog(null, "用户名或密码错误！", "登录出错", JOptionPane.ERROR_MESSAGE);
            }
        }
        if (e.getSource() == btn2){
            MyController.getRegister();
        }
    }
}
